package com.admin.system.controller;

import com.admin.base.BaseController;
import com.admin.base.Response;
import com.alibaba.fastjson.JSON;
import com.admin.system.entity.SystemGroup;
import com.admin.system.service.SystemGroupService;
import com.admin.system.vo.query.SystemGroupQuery;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 组织机构
 */
@Controller
@RequestMapping(value = "/systemGroup")
public class SystemGroupController extends BaseController {

    @Value("classpath:json/treeData.json")
    private Resource areaRes;
    @Autowired
    SystemGroupService systemGroupService;

    /**
     * 列表页面
     */
    @RequestMapping(value = "/list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        mav.setViewName("system/systemGroup/list");
        return mav;
    }

    /**
     * 分页查询数据
     */
    @RequestMapping(value = "/paging")
    @ResponseBody
    public Response paging(HttpServletRequest request, HttpServletResponse response, SystemGroupQuery query) throws IOException {
        List<SystemGroup> list = systemGroupService.listByConditions(query);
        return Response.ok(list);
//    File file = areaRes.getFile();
//    String s = jsonRead(file);
//    System.out.println(request.getContextPath());
//    JSONObject object = JSONObject.parseObject(s);
//    List<Person> list = JSONObject.parseArray(object.getString("data"), Person.class);
//    return Response.ok(list);
    }

    /**
     * 新增页面
     */
    @RequestMapping(value = "/toAdd")
    public ModelAndView toAdd(HttpServletRequest request, HttpServletResponse response, ModelAndView mav,
                              @RequestParam(value = "id", required = true) Integer id) {
        if (id.equals(0)) {
            SystemGroup group = getRoot();
            mav.addObject("systemGroup", group);
        } else {
            SystemGroup group = systemGroupService.queryById(id);
            mav.addObject("systemGroup", group);
        }
        mav.setViewName("system/systemGroup/add");
        return mav;
    }

    /**
     * 保存数据
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Response add(HttpServletRequest request, HttpServletResponse response, SystemGroup systemGroup) {
        systemGroupService.add(systemGroup);
        return Response.ok();
    }

    /**
     * 删除数据
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response, Integer[] id) {
        systemGroupService.delete(id);
        return Response.ok();
    }

    /**
     * 编辑页面
     */
    @RequestMapping(value = "/toEdit")
    public ModelAndView toEdit(HttpServletRequest request, HttpServletResponse response, ModelAndView mav, Integer id) {
        SystemGroup systemGroup = systemGroupService.queryById(id);
        if (systemGroup.getPid() == 0) {
            mav.addObject("psystemGroup", getRoot());
        } else {
            SystemGroup psystemGroup = systemGroupService.queryById(systemGroup.getPid());
            mav.addObject("psystemGroup", psystemGroup);
        }


        mav.addObject(systemGroup);
        mav.setViewName("system/systemGroup/edit");
        return mav;
    }

    /**
     * 详情页面
     */
    @RequestMapping(value = "/detail")
    public ModelAndView detail(HttpServletRequest request, HttpServletResponse response, ModelAndView mav, Integer id) {
        SystemGroup systemGroup = systemGroupService.queryById(id);
        mav.addObject(systemGroup);
        mav.setViewName("system/systemGroup/detail");
        return mav;
    }

    /**
     * 更新数据
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Response update(HttpServletRequest request, HttpServletResponse response, SystemGroup systemGroup) {
        systemGroupService.update(systemGroup);
        return Response.ok();
    }

    @RequestMapping(value = "/toStructure")
    public ModelAndView toStructure(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        mav.setViewName("system/systemGroup/structure");
        List<SystemGroup> list = systemGroupService.getGroupTree();
        Object zNodes = JSON.toJSON(list);
        mav.addObject("zNodes", zNodes);
        return mav;
    }

    /**
     * 调整组织机构结构
     * @param request
     * @param response
     * @param nodes
     * @return
     */
    @RequestMapping(value = "/structure")
    @ResponseBody
    public Response structure(HttpServletRequest request, HttpServletResponse response, String nodes) {
        List<SystemGroup> groups = JSON.parseArray(nodes,SystemGroup.class);
        systemGroupService.saveGroupTree(groups);
        return Response.ok();
    }

    /**
     * 选择组织机构的页面
     * @param request
     * @param response
     * @param mav
     * @return
     */
    @RequestMapping(value = "/toChoice")
    public ModelAndView toChoice(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        mav.setViewName("system/systemGroup/choice");
        List<SystemGroup> list = systemGroupService.getGroupTree();
        Object zNodes = JSON.toJSON(list);
        mav.addObject("zNodes", zNodes);
        return mav;
    }

    private SystemGroup getRoot() {
        SystemGroup group = new SystemGroup();
        group.setId(0);
        group.setGroupName("根");
        return group;
    }
}
